package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.ysd.dao.impl.SheJiShiErDaoImpl;
import com.ysd.entity.SheJiShi;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminSheJiShiServlet extends BaseServlet {
	/**
	 * 查询所有设计师
	 * localhost:8080/shangcheng/adminSheJiShi?reqName=getSheJiShi
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getSheJiShi(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		SheJiShiErDaoImpl sjsDao = new SheJiShiErDaoImpl();
		int page=0;
		if(request.getParameter("page")!=null && !request.getParameter("page").equals("")) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		int size=0;
		if(request.getParameter("size")!=null && !request.getParameter("size").equals("")) {
			size = Integer.parseInt(request.getParameter("size"));
		}
		
		List<SheJiShi> sjsList = sjsDao.getSheJiShiListByPage(page, size);// 分页查到所有设计师集合

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		Gson gson = new Gson();
		String res = gson.toJson(sjsList);

		out.print(res); // 响应数据

		out.flush();
		out.close();
	}
	/**
	 * localhost:8080/shangcheng/adminSheJiShi?reqName=delSheJiShi
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delSheJiShi(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int cId = Integer.parseInt(request.getParameter("sheJiShiid"));
		SheJiShiErDaoImpl sjsDao = new SheJiShiErDaoImpl();
		int n =sjsDao.deletesheJiShiid(cId); // 调用删除的方法
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		if (n > 0) { // 删除成功
			out.print(Result.toClient(true, "删除成功", ""));// {"success:true,"message":"删除成功","remark":""}
			
		} else { // 删除失败
			out.print(Result.toClient(false, "删除失败", ""));// {"success:false,"message":"删除失败","remark":""}
		}
	}
	
	/**
	 * 批量删除
	 * localhost:8080/shangcheng/adminSheJiShi?reqName=delSheJiShiids
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delSheJiShiids(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String sheJiShiids = request.getParameter("sheJiShiids");
		SheJiShiErDaoImpl sjsDao = new SheJiShiErDaoImpl();
		int n =sjsDao.deletesheJiShiids(sheJiShiids); // 调用删除的方法
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		if (n > 0) { // 删除成功
			out.print(Result.toClient(true, "删除成功", ""));// {"success:true,"message":"删除成功","remark":""}
			
		} else { // 删除失败
			out.print(Result.toClient(false, "删除失败", ""));// {"success:false,"message":"删除失败","remark":""}
		}
	}
	
	/**
	 * localhost:8080/shangcheng/adminSheJiShi?reqName=addSheJiShi
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addSheJiShi(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		SheJiShiErDaoImpl sjsDaoImpl = new SheJiShiErDaoImpl();// 实现类对象

		String sheJiShiName = request.getParameter("sjsName");// 接受前台传来的参数
		String zhiCheng = request.getParameter("sjsZhiCheng");
		String sheJiShiTX = request.getParameter("sjsTX");
		if (sheJiShiName != null && !sheJiShiName.equals("")) {
			List<SheJiShi> sjsList = sjsDaoImpl.getSheJiShiByName(sheJiShiName);
			System.out.println(sjsList);
			if (sjsList.size() == 0) {// 没有查到,可以添加
				SheJiShi sjs = new SheJiShi(0, sheJiShiName, zhiCheng, sheJiShiTX);// 创建带参构造方法
				System.out.println(sjs);

				int n = sjsDaoImpl.addSheJiShi(sjs);
				if (n > 0) {
					out.print(Result.toClient(true, "添加成功", ""));
				} else {
					out.print(Result.toClient(false, "添加失败", ""));
				}
			} else {// 查到了,不能添加
				out.print(Result.toClient(false, "设计师已存在", ""));
			}
		}

		out.flush();
		out.close();
	}

	/**
	 * localhost:8080/shangcheng/adminSheJiShi?reqName=upload
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void upload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		// 1.创建SmartUpload对象
		SmartUpload smart = new SmartUpload();

		// 2.设置相应的编码格式
		smart.setCharset("UTF-8");

		// 3.初始化
		smart.initialize(getServletConfig(), request, response);
		try {
			// 4.上传
			smart.upload();
			// 5.设置上传文件名字
			String fileName = System.currentTimeMillis() + ".jpg";
			// 将文件保存到指定目录
			smart.getFiles().getFile(0).saveAs("/images/" + fileName);
			// 响应
			out.print(Result.toClient(true, "上传成功", fileName));
		} catch (SmartUploadException e) {
			e.printStackTrace();
			out.print(Result.toClient(true, "上传失败", "图片过大,无法上传"));
		}
		out.flush();
		out.close();
	}

	/**
	 * localhost:8080/shangcheng/adminSheJiShi?reqName=getSheJiShiById
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getSheJiShiById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		SheJiShiErDaoImpl sjsDaoImpl = new SheJiShiErDaoImpl();// 实现类对象
		int sheId = 0;
		if (request.getParameter("sId") != null && !request.getParameter("sId").equals("")) {
			sheId = Integer.parseInt(request.getParameter("sId"));// 接受前台传来的Id,并转化为int类型
		}

		SheJiShi sjs = sjsDaoImpl.getSheJiShiById(sheId);// 调用实现类的根据设计师Id查询设计师

		Gson gson = new Gson();
		String res = gson.toJson(sjs); // 将sjs对象转化为json字符串
		out.print(res); // 响应数据

		out.flush();
		out.close();
	}

	/**
	 * localhost:8080/shangcheng/adminSheJiShi?reqName=updateSheJiShiById
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateSheJiShiById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		SheJiShiErDaoImpl sjsDaoImpl = new SheJiShiErDaoImpl();// 实现类对象
		int sheJiShiid = 0;
		if (request.getParameter("sjsId") != null && !request.getParameter("sjsId").equals("")) {
			sheJiShiid = Integer.parseInt(request.getParameter("sjsId"));
		}
		
		String sheJiShiName = request.getParameter("sjsName");// 接受前台传来的参数
		String zhiCheng = request.getParameter("sjsZC");
		String sheJiShiTX = request.getParameter("sjsTX");
		SheJiShi sjs = new SheJiShi(sheJiShiid, sheJiShiName, zhiCheng, sheJiShiTX);
			
		int n = sjsDaoImpl.updateSheJiShiById(sjs);
			
		if (n > 0) {
			out.print(Result.toClient(true, "修改成功", ""));
		} else {
			out.print(Result.toClient(false, "修改失败", ""));
		}
		out.flush();
		out.close();
				
	}
	
}
